#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rest_framework.pagination import PageNumberPagination
from rest_framework.decorators import action

from apps.interface.models import Interface
from apps.interface.serializers import InterfaceSerializers, InterfaceListSerializers
from utils.rest.views import CustomViewBase
from utils.rest.response import JsonResponse


class InterfacePagination(PageNumberPagination):
    """
    测试项目列表自定义分页
    """

    # 默认每页显示的个数
    page_size = 10
    # 可以动态改变每页显示的个数
    page_size_query_param = 'limit'
    # 页码参数
    page_query_param = 'page'
    # 最多能显示多少页
    max_page_size = 100


class InterfaceViewSet(CustomViewBase):
    """
    HTTP接口视图

    """
    queryset = Interface.objects.all().order_by('-edit_time')
    # 分页
    serializer_class = InterfaceSerializers
    # filter_backends = (DjangoFilterBackend, )
    filter_fields = ('project', )
    pagination_class = InterfacePagination
    search_fields = ('id', 'name')

    def get_serializer_class(self):
        if self.action == 'list':
            return InterfaceListSerializers
        else:
            return InterfaceSerializers

    @action(methods=['get'], detail=False)
    def count(self, request):
        count = Interface.objects.count()
        return JsonResponse(data={'count': count}, msg='success', code=200)
